<?php
/**
 * Notes:
 * Created by PhpStorm.
 * Author: TS
 * Date: 2019/10/17
 * Time: 9:50
 */

namespace app\model;


use think\Model;

class ChatList extends Model
{
    public function add($data){
        return self::create($data);
    }

    public function del($chat_list_id){
        return self::get($chat_list_id)->delete();
    }

    public static function edit($chat_list_id,$data){
        $res = self::update($data,['chat_list_id' => $chat_list_id]);
        return $res;
    }


    public function getByUser($user_id){
        return self::join('users','users.user_id=chat_list.chat_target')
            ->join('friends','users.user_id = friends.friend_target and friends.user_id = ' .$user_id )
            ->where([
                'chat_list.user_id' => $user_id,
                'chat_list.type' => '1',
            ])
            ->order('push_time','desc')
            ->field(['chat_list.*,chat_list.push_time as today_time,user_name,users.user_header,friends.*'])
            ->select();
    }
    public function getByGroupChat($user_id){
        return self::where([
            'user_id' => $user_id,
            'type' => '2'
        ])->join('group_chat','group_chat.group_chat_id = chat_list.chat_target')
            ->field('group_chat_id,group_chat_name,type,chat_content,chat_num,push_time')->select();
    }

    public function checkPush($user_id,$target){
        return self::get([
            'user_id' => $user_id,
            'chat_target' => $target
        ]);
    }
    public function getPushAndUser($chat_target,$friend_id){
        return self::join('friends','chat_list.user_id = friends.friend_target')
            ->where([
                'chat_list.user_id' => $chat_target,
                'chat_list.chat_target' => $friend_id
            ])
            ->find();
    }
    public function getPushTimeAttr($value){
        return date('H:i', $value);
    }
    public function getTodayTimeAttr($value){
        return date('m-d H:i', $value);
    }




}